﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;

namespace Network
{
   internal class HeartbeatSender
   {
      ////////////////////////////////////////////////////////////////////////
      //
      private static Logger logger_;
      private string senderId_;
      private string displayName_;
      private int heartbeatSeconds_;
      private bool isAlive_;
      private bool wasAlive_;
      private DateTime lastAlive_;
      private string statusText_;
      private bool isClient_;
      private string planet_;

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <param name="senderId"></param>
      public HeartbeatSender(int heartbeatSeconds, string senderId)
      {
         logger_ = Program.Logger;
         heartbeatSeconds_ = heartbeatSeconds;
         senderId_ = senderId;
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      public string SenderId
      {
         get { return senderId_; }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <returns>false if it's too long since the last heartbeat</returns>
      public bool IsAlive
      {
         get { return isAlive_ && ((DateTime.Now - lastAlive_).TotalSeconds < heartbeatSeconds_); }
         set { isAlive_ = value; }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public bool WasAlive
      {
         get { return wasAlive_; }
         set { wasAlive_ = value; }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public string DisplayName
      {
         get { return displayName_; }
         set { displayName_ = value; }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public bool IsClient
      {
         get { return isClient_; }
         set { isClient_ = value; }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public string Planet
      {
         get { return planet_; }
         set { planet_ = value; }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <returns></returns>
      public string StatusText
      {
         get { return statusText_; }
         set { statusText_ = value; }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <param name="now"></param>
      /// <returns>true if the state changes</returns>
      public bool SetLastAlive(DateTime heartbeatSentAt)
      {
         isAlive_ = true;
         lastAlive_ = heartbeatSentAt;
         return WasAlive != IsAlive;
      }
   }
}
